package com.pkk.penguin.test.elasticsearch.service;

import com.pkk.penguin.test.elasticsearch.entity.Article;
import com.pkk.penguin.test.elasticsearch.repository.ArticleRepository;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

/**
 * @description: 文章业务实现类
 * @author: peikunkun
 * @create: 2019-10-18 15:42
 **/
@Slf4j
@Service
public class ArticleService {


  @Autowired
  private ArticleRepository articleRepository;

  /**
   * @Description: 查询文章信息
   * @Param: [id]
   * @return: com.pkk.penguin.test.elasticsearch.entity.Article
   * @Author: peikunkun
   * @Date: 2019/10/18 0018 下午 3:18
   */
  public Article queryArticleInfoById(Long id) {
    return articleRepository.queryArticleInfoById(id);
  }


  /**
   * @Title: queryArticleByTitleAndContent<br>
   * @Description: <查询文章依据标题或内容><br>
   * @author peikunkun<br>
   * @date 2017年2019/10/18 0018 下午 3:24<br>
   * @URL:[]
   * @version V1.0<br>
   */
  public List<Article> queryArticleByTitleContentAnnotation(String title, String content) {
    return articleRepository.queryArticleByTitleContentAnnotation(title, content);
  }


  /**
   * @Title: queryArticleByTitleAndContent<br>
   * @Description: <查询文章依据标题或内容><br>
   * @author peikunkun<br>
   * @date 2017年2019/10/18 0018 下午 3:24<br>
   * @URL:[]
   * @version V1.0<br>
   */
  public List<Article> queryArticleByTitleContentSimple(String title, String content) {
    //return articleRepository.queryArticleByBaseTitleContent(title, content);
    return null;
  }

  /**
   * @Title: queryArticleByTitleAndContent<br>
   * @Description: <查询文章依据标题或内容><br>
   * @author peikunkun<br>
   * @date 2017年2019/10/18 0018 下午 3:24<br>
   * @URL:[]
   * @version V1.0<br>
   */
  public Page<Article> queryArticleByTitleContentBuild(String title, String content) {
    QueryBuilder queryBuilder = QueryBuilders.boolQuery()
        .must(QueryBuilders.matchQuery("title", title))
        .must(QueryBuilders.matchQuery("content", content));

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(queryBuilder)
        .build();
    return articleRepository.search(searchQuery);
  }


  /**
   * @Description: 查询文章匹配
   * @Param: [title, content]
   * @return: org.springframework.data.domain.Page<com.pkk.penguin.test.elasticsearch.entity.Article>
   * @Author: peikunkun
   * @Date: 2019/10/18 0018 下午 5:23
   */
  public Page<Article> queryArticle(String title, String content) {
    QueryBuilder queryBuilder = QueryBuilders.boolQuery();
    if (StringUtils.isNotBlank(title)) {
      ((BoolQueryBuilder) queryBuilder).should(QueryBuilders.matchPhraseQuery("title", title));
    }
    if (StringUtils.isNotBlank(content)) {
      ((BoolQueryBuilder) queryBuilder).should(QueryBuilders.matchPhraseQuery("content", content));
    }
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(queryBuilder)
        .build();
    return articleRepository.search(searchQuery);
  }


  /**
   * @Description: 查询文章匹配分页
   * @Param: [title, content]
   * @return: org.springframework.data.domain.Page<com.pkk.penguin.test.elasticsearch.entity.Article>
   * @Author: peikunkun
   * @Date: 2019/10/18 0018 下午 5:23
   */
  public Page<Article> queryArticlePage(String title, String content, Integer pageNum, Integer pageSize) {
    QueryBuilder queryBuilder = QueryBuilders.boolQuery();
    if (StringUtils.isNotBlank(title)) {
      ((BoolQueryBuilder) queryBuilder).should(QueryBuilders.matchPhraseQuery("title", title));
    }
    if (StringUtils.isNotBlank(content)) {
      ((BoolQueryBuilder) queryBuilder).should(QueryBuilders.matchPhraseQuery("content", content));
    }
    SearchQuery searchQuery = new NativeSearchQuery(queryBuilder);
    searchQuery.setPageable(PageRequest.of((pageSize - 1), pageNum, new Sort(Direction.DESC, "id")));
    return articleRepository.search(searchQuery);
  }


}
